package hr.fer.apr.ht41337.lab4;

import java.util.TreeMap;

import jmatlink.JMatLink;

public class Glavni {
	
	public static void main(String[] args) throws Exception {
		
		if (args.length != 4)
			throw new IllegalArgumentException("ocekuju se 4 argumenta");
		
		double korak = Double.parseDouble(args[0]);
		double interval = Double.parseDouble(args[1]);
		
		Matrica A = Matrica.read("A.txt");
		Matrica B = Matrica.read("B.txt");
		Matrica x0 = Matrica.read("x0.txt");
		
		NumerickaIntegracija sustav = new NumerickaIntegracija(A,B,korak);
		sustav.setIspis(Boolean.parseBoolean(args[2]));
		sustav.setBrojIteracija(Integer.parseInt(args[3]));
//		TreeMap<Double, Matrica> temp = sustav.TrapezniPostupak(x0, interval);
		TreeMap<Double, Matrica> temp = sustav.RungeKutta(x0, interval);
		
		JMatLink engine = new JMatLink();
		engine.engOpen();
		
		iscrtavanjeMatlab(temp, engine);
		System.in.read();
		engine.engClose();
	}

	private static void iscrtavanjeMatlab (TreeMap<Double, Matrica> temp, JMatLink engine) {
		double[] x = new double[temp.size()];
		double[][] y = new double[temp.size()][temp.firstEntry().getValue().rows];
		Double[] x_temp = temp.keySet().toArray(new Double[0]);
		for (int i = 0; i < x_temp.length; i++) {
			x[i] = x_temp[i].doubleValue();
			Matrica y_temp = temp.get(x_temp[i]);
			for (int j = 0; j < y_temp.rows; j++)
				y[i][j] = y_temp.get(j, 0);
		}
		engine.engPutArray("x", x);
		engine.engPutArray("y", y);
		engine.engEvalString("plot(x,y)");
	}
}
